from django.shortcuts import render
from django.views.decorators.http import *
from django.http import HttpResponse
from django.views.generic import View
from django.conf import settings
from django.http import HttpResponseRedirect
from .forms import companyForm,jobForm
from kkoffer.user.models import company,job,tag,deliver,user
from django.conf import settings
from django.http import JsonResponse
from kkoffer.tool.views import login_required
from django.utils.translation import ugettext as _

class addJob(View):
    def get(self,request):
        context = {}
        uid = request.session['id']
        obj = company.objects.get(pk=uid)
        CITY = {
            'china':{
                'beijing':_('beijing'),
                'shanghai':_('shanghai'),
                'shenzhen':_('shenzhen'),
                'guangzhou':_('guangzhou'),
                'dalian':_('dalian'),
            },
            'japan':{
                'tokyo':_('tokyo'),
                'osaka':_('osaka'),
            }
        }
        context["jobTag"] =["on a business trip","work less 30hour","urge employ","work use english"]
        classify = settings.CLASSIFY
        if not obj.status:
            return render(request,'prompt.html',{'text':_('corp checking')})
        if request.GET.get('id'):
            context = job.objects.filter(id = request.GET.get('id')).values()[0]
            if context['jpPayment'].find(',') > 0:
                context['sPay'] = context['jpPayment'].split('-')[0]
                context['ePay'] = context['jpPayment'].split('-')[1]
                del context['jpPayment']
            # import ipdb; ipdb.set_trace()
            context.update(company.objects.filter(pk = uid).values('country','location')[0])
            tagData = tag.objects.filter(reid = request.GET.get('id')).values('tag')
            context['tag'] = [str(x['tag']) for x in tagData]
            # import ipdb; ipdb.set_trace()
            context["jobTag"] =["on a business trip","work less 30hour","urge employ","work use english"]

        context['city'] = CITY
        context['classifyList'] = classify
        return render(request,'addJob.html',context)
    def post(self,request):
        post = request.POST.copy()
        post['jpPayment'] = post.get('jpPayment') or str(post['sPay']) + '-' + str(post['ePay'])
        post['uid_id'] = request.session['id']
        
        if request.POST.get('id'):
            id = request.POST.get('id')
            obj = job.objects.get(pk=id)
            f = jobForm(post,instance=obj)
        else:
            f = jobForm(post)
        if f.is_valid():
            obj = f.save(commit=False)
            obj.uid_id = request.session['id']
            f.save()
        tag.objects.filter(reid = obj.pk).delete()
        queryList = []
        for x in request.POST.getlist('tag[]'):
            queryList.append(tag(reid = obj.pk,type = 'job',tag = x))
        tag.objects.bulk_create(queryList)
        return HttpResponseRedirect('/company/job')

class basic(View):
    def get(self,request):
        id = request.session['id']
        context = company.objects.filter(uid_id = id).values()[0]
        COMPANYTAG = {
        'company':{
            'IPO':_('IPO'),
            'foreign-capital enterprise':_('foreign-capital enterprise'),
            'Female staff above':_('Female staff above'),
            'clothes freedom':_('clothes freedom'),
            'employee stock ownership':_('employee stock ownership'),
            'flex':_('flex'),
            'career guide':_('career guide'),
            'active club':_('active club'),
        },
        'welfare':{
            'pay carfare':_('pay carfare'),
            'pay night shift':_('pay night shift'),
            'pay rent':_('pay rent'),
            'child birth':_('child birth'),
            'The wedding funeral subsidy':_('The wedding funeral subsidy'),
            'qualification reward':_('qualification reward')
        },
        'insurance':{
            'unemployment insurance':_('unemployment insurance'),
            'casualty insurance' : _('casualty insurance'),
            'accident insurance on a business':_('accident insurance on a business'),
            'Special consolation':_('Special consolation'),
            'physical checkup':_('physical checkup'),
            'pension':_('pension'),
            'health insurance':_('health insurance'),
            'team life insurance':_('team life insurance')
        },
        'vacation':{
            'combined holidays above 5 days':_('combined holidays above 5 days'),
            'summer vacation':_('summer vacation'),
            'The wedding funeral vacation':_('The wedding funeral vacation'),
            'Early end of the year':_('Early end of the year'),
            'Paid leave 10 days ~20 days':_('Paid leave 10 days ~20 days'),
            'maternity leave':_('maternity leave'),
            'Once a year take 120 days holiday':_('Once a year take 120 days holiday'),
            }
        }
        CITY = {
            'china':{
                'beijing':_('beijing'),
                'shanghai':_('shanghai'),
                'shenzhen':_('shenzhen'),
                'guangzhou':_('guangzhou'),
                'dalian':_('dalian'),
            },
            'japan':{
                'tokyo':_('tokyo'),
                'osaka':_('osaka'),
            }
        }
        # context['company'] = settings.COMPANYTAG
        context['company'] = COMPANYTAG
        context['city'] = CITY
        context['industryList'] = company._meta.get_field('industryType').choices
        tagData = tag.objects.filter(reid = id).values('tag')
        context['tag'] = [str(x['tag']) for x in tagData]
        if request.GET.get('edit',''):
            return render(request,'prompt.html',context)
        return render(request,'basic.html',context)
    def post(self,request):
        id = request.session['id']
        obj = company.objects.filter(uid_id = id)
        f = companyForm(request.POST,instance=obj[0])
        tag.objects.filter(reid = id).delete()
        queryList = []
        for x in request.POST.getlist('tag[]'):
            queryList.append(tag(reid = id,type = 'company',tag = x))
        tag.objects.bulk_create(queryList)
        if f.is_valid():
            user.objects.filter(pk=id).update(name=f.cleaned_data['name'])
            f.save()
            return render(request,'prompt.html',{'text':_('corp checking')})
        else:
            return HttpResponseRedirect('/company/basic')

def jobList(request):
    context = {}
    context['list'] = job.objects.filter(uid = request.session['id'])
    return render(request,'job.html',context) 


def delJob(request):
    job.objects.filter(pk=request.GET.get('id')).delete()
    return HttpResponseRedirect('/company/job/')

def jobStatus(request,id,status):
    status = status == '1' and '0' or 1
    obj = job.objects.get(pk=id)
    obj.status = int(status)
    obj.save()
    return HttpResponseRedirect('/company/job/')

def unsuited(request,id):
    uid = request.session['id']
    deliver.objects.filter(comeFrom_id=id,to_id=uid).update(status=2)
    message.objects.create(to_id=id,content = 'cc')
    return HttpResponseRedirect('/company/job/')

def ignore(request,id):
    uid = request.session['id']
    deliver.objects.filter(comeFrom_id=id,to_id=uid).update(status=2)
    return HttpResponseRedirect('/company/job/')

def receive(request,id):
    context = {}
    context['list'] = deliver.objects.filter(job_id=id).select_related('comefrom')
    # ff = context['list'][0]
    return render(request,"receive.html",context)

def companyAudit(request,id=''):
    context = {}
    if id:
        company.objects.filter(pk=id).update(status = True)
    context['list'] = company.objects.filter(status=False).values()
    return render(request,'audit.html',context)

def companyDel(request,id):
    context = {}
    user.objects.get(pk=id).re















